package com.itheima.controller;

import com.itheima.health.dto.UserDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.User;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;



@RestController
public class UserController {

    @Reference
    private UserService userService;



    @PostMapping("user/findPage")
    //  @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = userService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }


    @PostMapping("user/add")
    public Result add(@RequestBody UserDTO userDTO) {
        try {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String password = passwordEncoder.encode(userDTO.getPassword());
            userDTO.setPassword(password);
            userService.saveUser(userDTO);
            if (StringUtils.isEmpty(userDTO.getId())) {
                //  insert
                return new Result(true, MessageConstant.ADD_USER_SUCCESS);
            } else {
                // update
                return new Result(true, MessageConstant.UPDATE_USER_SUCCESS);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败！");
        }
    }

    @GetMapping("user/findroleInfoByUserId/{id}")
    public Result findroleInfoByUserId(@PathVariable("id") int id) {
        try {
            Map map = userService.findroleInfoByUserId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @DeleteMapping("user/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable("id") int id) {
        try {

            User user = new User();

            user.setIs_delete(1);  //  逻辑删除
            user.setId(id);
            userService.updateById(user);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

}
